#include "FoodOderWindow.h"
#include "ui_FoodOderWindow.h"
#include "FoodTable.h"
#include "Food.h"
#include "foodorderitem.h"
#include "ui_FoodOrderItem.h"

#include <QListWidgetItem>

FoodOderWindow::FoodOderWindow(QWidget *parent) :
    QListWidget(parent),
    ui(new Ui::FoodOderWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("订餐界面");   //设置标题
    /**
      * @leiteorz
      * 加载食物列表
      */
    FoodTable food_table;
    Food* foodList = new Food[100];
    int i = 0;
    foodList = food_table.queryAllUnnullFood();    //查询所有未下架食物
    while(foodList[i].name != ""){
//        qDebug()<<foodList[i].name<<" "<<foodList[i].price<<" ";
        /**
          * @leiteorz
          * 绘制ListItemm
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(798,89));
        FoodOrderItem *foodOrderItem = new FoodOrderItem();
        foodOrderItem->init(foodList[i].name,foodList[i].price);
        this->setItemWidget(item,foodOrderItem);
        connect(foodOrderItem,SIGNAL(addIntoShopCart(Food)),this,SLOT(add_into_shop_cart(Food)));
        i++;
    }
}

FoodOderWindow::~FoodOderWindow()
{
    delete ui;
}

//显示订餐界面
void FoodOderWindow::replyShowFoodOrder(){
    this->show();
}

//返回按钮点击事件:返回欢迎界面
void FoodOderWindow::on_back_btn_clicked(){
    emit showWelcomWindow();
    this->hide();
}

//显示购物车界面
void FoodOderWindow::on_buy_cart_btn_clicked(){
    emit showShopCartWindow();
}

//刷新ui
void FoodOderWindow::data_changed(){
    this->clear();  //先删掉前面列表的所有内容
    /**
      * @leiteorz
      * 重新遍历数据库
      * 加载食物列表
      */
    FoodTable food_table;
    Food* foodList = new Food[100];
    int i = 0;
    foodList = food_table.queryAllUnnullFood();    //查询所有未下架食物
    while(foodList[i].name != ""){
//        qDebug()<<foodList[i].name<<" "<<foodList[i].price<<" ";
        /**
          * @leiteorz
          * 绘制ListItemm
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(798,89));
        FoodOrderItem *foodOrderItem = new FoodOrderItem();
        foodOrderItem->init(foodList[i].name,foodList[i].price);
        this->setItemWidget(item,foodOrderItem);
        i++;
    }
}

//发送信号给购物车,通知添加了新的食物
void FoodOderWindow::add_into_shop_cart(Food food){
    emit signal_add_intoshop_cart(food);
}
